package com.homegoods.classify.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.home.R;
import com.homegoods.home.presenters.SearchResultsListPresenter;
import com.homegoods.resource.SpacesItemDecoration;
import com.homegoods.resource.pullDownMenuAdapter.SwitchConditionAdapter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.resource.util.MultiPullDownMenuProxy;
import com.homegoods.resource.util.Utils;
import com.kedll.library.base.KRefreshListActivity;
import com.kedll.library.util.MyUtils;
import com.homegoods.home.databinding.HomeActivitySearchBrandBinding;

import java.util.Arrays;


/**
 * @author QiQi
 */
@Route(path = ARouterPath.PATH_SEARCH_BRAND)
public class SearchByBrandActivity extends KRefreshListActivity<HomeActivitySearchBrandBinding, SearchResultsListPresenter> {

    /**
     * 综合排序
     */
    private final static String[] ALL = new String[]{
            "综合",
            "上新时间"
    };
    @Autowired(name = "searchContent")
    public String searchContent;
    @Autowired(name = "sid")
    public String sid;

    private MultiPullDownMenuProxy multiPullDownMenuProxy;
    private SwitchConditionAdapter allAdapter;

    private int saleClick;
    private int priceClick;

    private boolean flag = false;
    private String type = "";   //NowPriceOW 价格 A=升序 B=降序 SaleCountOW 销量 A=升序 B=降序

    @Override
    protected int getLayout() {
        return R.layout.home_activity_search_brand;
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        ARouter.getInstance().inject(this);
        setTitle(searchContent);
        setTopRightButton(null, R.mipmap.su, v -> ARouter.getInstance()
                .build(ARouterPath.PATH_SEARCH)
                .navigation());
    }

    @Override
    protected SearchResultsListPresenter getPresenter() {
        return new SearchResultsListPresenter(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        appBarLayout.setBackgroundResource(R.color.color_f6f6f6);

        list.addItemDecoration(new SpacesItemDecoration(2, 8));


        list.setPadding(0, MyUtils.dp2px(8), 0, 0);
        list.setClipToPadding(false);
        initPullDownMenu();
        Log.d(TAG, "initData: sid == " + sid);
        mPresenter.updateContent(sid);
    }

    private void initPullDownMenu() {
        multiPullDownMenuProxy = new MultiPullDownMenuProxy(mBinding.pulldown, R.color.color_a541c9, R.color.color_333333);
        allAdapter = new SwitchConditionAdapter(this, new PullDownClickListener(mPresenter, ALL, mBinding.tvAll));
        allAdapter.setData(Arrays.asList(ALL));
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return Utils.getGridLayoutManager(getContext(), adapter, 2);
    }


    @Override
    protected void initEvent() {
        Log.d(TAG, "initEvent: sid ==  " + sid);
        mBinding.llAll.setOnClickListener(this::onAllClick);
        mBinding.llSale.setOnClickListener(this::onSaleClick);
        mBinding.llPrice.setOnClickListener(this::onPriceClick);
    }

    public void onAllClick(View view) {
        multiPullDownMenuProxy.switchAttachView(allAdapter, mBinding.llAll);
        saleClick = initDefault(saleClick, mBinding.ivSaleUp, mBinding.ivSaleDown);
        priceClick = initDefault(priceClick, mBinding.ivPriceUp, mBinding.ivPriceDown);
    }

    private int initDefault(int clickCount, ImageView up, ImageView down) {
        if (clickCount > 0) {
            Drawable upDrawable = up.getDrawable();
            Drawable downDrawable = down.getDrawable();
            setDrawableTint(downDrawable, R.color.color_333333);
            setDrawableTint(upDrawable, R.color.color_333333);
        }
        return 0;
    }

    private void setDrawableTint(@NonNull Drawable upDrawable, @ColorRes int color) {
        DrawableCompat.setTint(upDrawable, ContextCompat.getColor(this, color));
    }

    public void onSaleClick(View view) {
        //关闭下拉菜单
        onClosePullMenu();
        //清价格排序
        priceClick = initDefault(priceClick, mBinding.ivPriceUp, mBinding.ivPriceDown);
        //切换销量排序
        saleClick = switchUpDown(saleClick, mBinding.ivSaleUp, mBinding.ivSaleDown);
//        mPresenter.updateContent(searchContent);
        type = flag ? "A" : "B";
        mPresenter.setMyType("SaleCountOW", type);
        mPresenter.updateContent(sid);
    }

    /**
     * 关闭下拉菜单
     */
    private void onClosePullMenu() {
        if (mBinding.pulldown.isExpand()) {
            mBinding.pulldown.onCollapse(mBinding.llAll);
        }
    }

    /**
     * 切换上下箭头显示
     *
     * @param clickCount
     * @param up
     * @param down
     * @return
     */
    private int switchUpDown(int clickCount, ImageView up, ImageView down) {
        clickCount++;
        Drawable upDrawable = up.getDrawable();
        Drawable downDrawable = down.getDrawable();
        if (clickCount % 2 == 1) {
            setDrawableTint(upDrawable, R.color.color_a541c9);
            setDrawableTint(downDrawable, R.color.color_333333);
            flag = false;
        } else {
            setDrawableTint(downDrawable, R.color.color_a541c9);
            setDrawableTint(upDrawable, R.color.color_333333);
            flag = true;
        }

        return clickCount;
    }

    public void onPriceClick(View view) {
        onClosePullMenu();
        saleClick = initDefault(saleClick, mBinding.ivSaleUp, mBinding.ivSaleDown);
        priceClick = switchUpDown(priceClick, mBinding.ivPriceUp, mBinding.ivPriceDown);
        type = flag ? "A" : "B";
        mPresenter.setMyType("NowPriceOW", type);
        mPresenter.updateContent(sid);
//        mPresenter.updateContent(searchContent);
    }

    /**
     * 下拉菜单选中回调
     */
    private static final class PullDownClickListener implements SwitchConditionAdapter.OnItemClickListener {

        private SearchResultsListPresenter designerPresenter;
        private String[] data;
        private TextView conditionView;

        PullDownClickListener(SearchResultsListPresenter designerPresenter, String[] data, TextView conditionView) {
            this.designerPresenter = designerPresenter;
            this.data = data;
            this.conditionView = conditionView;
        }

        @Override
        public void onItemClick(int position) {
            conditionView.setText(data[position]);
            designerPresenter.updateContent(data[position]);
            designerPresenter.setMyType("", "");
        }
    }
}
